const util = require('../../utils/util.js');
const api = require('../../config/api.js');
const user = require('../../services/user.js');

//获取应用实例
const app = getApp()
Page({
    data: {
        newGoods: [],
        hotGoods: [],
        topics: [],
        brands: [],
        floorGoods: [],
        banner: [],
        // channel: [],
        sury_showModal:false, // 显示modal弹窗
        CosumerFormAns:[],
        CosumerForm:{},
        combos:[],
        close_img:'http://img.zaodianjian.top/image/common/close_wite.png',
        cbs_showModal:false, // 显示modal弹窗
    },
    //小程序分享
    onShareAppMessage: function () {
        wx.showShareMenu({ 
            withShareTicket: true, 
            menus: ['shareAppMessage', 'shareTimeline'] 
          }) 
        return {
            title: '早点 · 见',
            desc: '早点 · 见',
            path: '/pages/index/index'
        }
    },onShareTimeline: function () {//用户点击右上角分享朋友圈 
        return { 
          title: '早点 · 见', 
          query: "id=110101&name=heyzqt", 
          imageUrl: '/static/images/logo.png' 
        } 
    }, onPullDownRefresh() {
        // 增加下拉刷新数据的功能
        var self = this;
        self.getIndexData();
    },
    getIndexData: function () {
        let that = this;
        var data = new Object();
        // // 新品首发
        // util.request(api.IndexUrlNewGoods).then(function (res) {
        //     if (res.errno === 0) {
        //         data.newGoods = res.data.newGoodsList
        //         that.setData(data);
        //     }
        // });
        // // 人气推荐
        // util.request(api.IndexUrlHotGoods).then(function (res) {
        //     if (res.errno === 0) {
        //         data.hotGoods = res.data.hotGoodsList
        //         that.setData(data);
        //     }
        // });
        // 专题精选
        // util.request(api.IndexUrlTopic).then(function (res) {
        //     if (res.errno === 0) {
        //         data.topics = res.data.topicList
        //         that.setData(data);
        //     }
        // });
        //品牌制造直供
        util.request(api.IndexUrlBrand).then(function (res) {
            if (res.errno === 0) {
                data.brand = res.data.brandList
                that.setData(data);
            }
        });
        // //详单
        // util.request(api.IndexUrlCategory).then(function (res) {
        //     if (res.errno === 0) {
        //         data.floorGoods = res.data.categoryList
        //         that.setData(data);
        //     }
        // });
        //首页banner
        util.request(api.IndexUrlBanner).then(function (res) {

            if (res.errno === 0) {
                data.banner = res.data.banner
                that.setData(data);
            }
        });
        // //列表
        // util.request(api.IndexUrlChannel).then(function (res) {
        //     if (res.errno === 0) {
        //         data.channel = res.data.channel
        //         that.setData(data);
        //     }
        // });
    },
    checkUserInfo: function (){
        if(wx.getStorageSync("userInfo")){
            console.log("用户授权了"); 
        }else{
            console.log("用户未授权"); 
            //进入login页面进行授权
            wx.redirectTo({
                url: '/pages/auth/btnAuth/btnAuth',
            })
        }
    },
    getConsumerForm:function(){
        let that = this;
        if(wx.getStorageSync('cosumerForm')){
            //调用推荐套餐接口
            this.getCombo();
            return;
        }
        //查看用户是否已填写过调查表
        util.request(api.questionnaireIsWrite).then(function(res){
            console.log(res.data);
            let result = res.data;
            if(result==true){
                wx.setStorageSync('cosumerForm', true);
            }else{
                // 若缓存中无数据展示用户调查表
                util.request(api.questionnaireGet).then(function (res) {
                    if (res.errno === 0) {
                        console.log(res.data);
                        //封装返回参数
                        let anss = [];
                        res.data.surveyQuestionVo.forEach(function(item,index){
                            let ans = {
                                "answerContent": "",
                                "questionId": item.id
                            }
                            anss.push(ans);
                        });
                        that.setData({
                            CosumerForm:res.data,
                            CosumerFormAns:anss
                        })
                        that.setData({
                            sury_showModal:true
                        });
                    }
                });
            }
        })         
    },
    //表单提交
    submit : function(e){
        var that = this;
        //判断是否填全
        try{
            that.data.CosumerFormAns.forEach(function(item,index){
                if(item.answerContent.length==0){
                    wx.showToast({
                        title: '请填充完整',
                        icon: 'none',
                        duration: 2000
                      });
                      throw 'Jump out now!'
                }
            });
        }catch (e) {
            console.log(e)
            return;
        }
        util.request(api.questionnaireSave,that.data.CosumerFormAns, 'POST', 'application/json').then(function (res) {
            wx.setStorageSync('cosumerForm', true);
            that.setData({
                sury_showModal:false
            });
        });
        console.log("submit提交");
        //调用推荐套餐接口
        this.getCombo();
      },
       //表单重置
      reset: function () {
        //清空参数
        this.data.CosumerFormAns.forEach(function(item,index){
            item.answerContent = "";
        });
      },
      //调查表单选
      radiochange:function(e){
        //获取第几个item
        let val = e.detail.value;
        //赋值
        let selected = "CosumerFormAns["+val.split("_")[0]+"].answerContent";
          this.setData({
            [selected]:val.split("_")[1]
        });
      },
      //调查表多选
      chechboxchange:function(e){
        //获取第几个item
        let val = e.detail.value;
        let checkVal = [];
        let dex = null;
        val.forEach(function(item,index){
            let v = item.split("_");
            dex = v[0];
            checkVal.push(v[1]);
        });
         //赋值
         let selected = "CosumerFormAns["+dex+"].answerContent";
         this.setData({
            [selected]:checkVal.toString()
        });
      },
      getCombo:function(e){
          var that = this;
        util.request(api.questionnaireReconmmendCombo).then(function (res) {
            console.log(res.data);
            that.setData({
                cbs_showModal:true,
                combos:res.data
            })
        });
      },
    /**
   * 生命周期函数--监听页面加载
   */
    onLoad: function (options) {
        //初次进入页面，需要验证用户是否授权
        // this.checkUserInfo();
        this.getIndexData();
        //验证调查表
        this.getConsumerForm();
    },
    onReady: function () {
        // 页面渲染完成
    },
    onShow: function () {
        // 页面显示
    },
    onHide: function () {
        // 页面隐藏
    },
    onUnload: function () {
        // 页面关闭
    },
})
